package com.example.smartall.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.ui.base.BaseActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;

import java.util.HashMap;

/**
 * 主页 我的 意见反馈
 */

public class FeedbackActivity extends BaseActivity {
    private ImageView imBack;
    private TextView tvTitle;
    private EditText etMsg;
    private Button btCommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        init();
        initEvent();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        etMsg = (EditText) findViewById(R.id.et_msg);
        btCommit = (Button) findViewById(R.id.bt_commit);

        tvTitle.setText("意见反馈");
    }

    @Override
    public void initEvent() {
        etMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.length() > 150) showToast("字数超过限制");
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FeedbackActivity.this.finish();
            }
        });
        btCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("".equals(etMsg.getText().toString().trim())){
                    showToast("先填写意见");
                    return;
                }else if (etMsg.getText().toString().length() > 150){
                    showToast("字数超过限制");
                    return;
                }
                HashMap<String,Object> params = new HashMap<>();
                params.put("content",etMsg.getText().toString());
                Api.config(ApiConfig.FEEDBACK,params)
                        .postRequest(new NetCallBack() {
                            @Override
                            public void onSuccess(String res) {
                                etMsg.setText("");
                                showToastSync("提交成功");
                            }

                            @Override
                            public void onFailure(Exception e) {
                                showToastSync("提交失败");
                            }
                        },SPUtils.config(FeedbackActivity.this, Constant.SP_USER_FILE_NAME).getString(Constant.TOKEN));
            }
        });
    }
}